﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CuaHangLaptop.BIZ;

namespace QuanLy.GUI
{
    public partial class Laptop_GUI : Form
    {
        List<Laptop_BIZ> list_lap = new List<Laptop_BIZ>();
        List<NhaSX_BIZ> list_nsx = new List<NhaSX_BIZ>();
        Laptop_BIZ current_lap = new Laptop_BIZ();
        bool updated = true;

        public Laptop_GUI()
        {
            InitializeComponent();
            GridView_KH.AutoGenerateColumns = false;
            Textbox_Disable(true);
            Frm_Load();
        }

        void Frm_Load()
        {
            NhaSX_BIZ nsx = new NhaSX_BIZ();
            list_lap = current_lap.GetAll_Laptop();
            list_nsx = nsx.GetAll_NSX();
            GridView_KH.DataSource = list_lap;
            cb_NSX.DataSource = list_nsx;
            cb_NSX.DisplayMember = "TenNSX";
            cb_NSX.ValueMember = "MaNSX";
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            current_lap = new Laptop_BIZ();
            Textbox_Disable(false);
            Reset_Val();
            updated = false;
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            try
            {
                if (!updated)
                {
                    current_lap = new Laptop_BIZ(-1, txt_Malaptop.Text, cb_NSX.SelectedValue.ToString(), txt_Tenlaptop.Text, txt_CPU.Text,
                                                txt_Ram.Text, txt_hdd.Text, txt_vga.Text, txt_monitor.Text,
                                                txt_bh.Text, txt_thongtin.Text,"", decimal.Parse(txt_Gia.Text));
                    list_lap.Add(current_lap);
                }
                else
                {
                    Get_Val();
                }
                current_lap.Save();
                GridView_KH.DataSource = null;
                GridView_KH.DataSource = list_lap;
                Textbox_Disable(true);
                updated = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi!" + ex.Message);
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GridView_KH_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (GridView_KH.Columns[e.ColumnIndex].HeaderText == "Xóa")
            {
                current_lap = (Laptop_BIZ)GridView_KH.Rows[e.RowIndex].DataBoundItem;
                list_lap.Remove(current_lap);
                current_lap.Delete();
                GridView_KH.DataSource = null;
                GridView_KH.DataSource = list_lap;
            }
            if (GridView_KH.Columns[e.ColumnIndex].HeaderText == "Sửa")
            {
                current_lap = (Laptop_BIZ)GridView_KH.Rows[e.RowIndex].DataBoundItem;
                Textbox_Disable(false);
            }
        }

        private void GridView_KH_SelectionChanged(object sender, EventArgs e)
        {
            if (GridView_KH.SelectedRows.Count > 0)
            {
                current_lap = (Laptop_BIZ)GridView_KH.SelectedRows[0].DataBoundItem;
                Set_Val(current_lap);
                Textbox_Disable(true);
            }
        }

        void Textbox_Disable(bool _enable)
        {
            txt_bh.ReadOnly = txt_CPU.ReadOnly = txt_Gia.ReadOnly = txt_hdd.ReadOnly
            = txt_Malaptop.ReadOnly = txt_monitor.ReadOnly = txt_Ram.ReadOnly
            = txt_Tenlaptop.ReadOnly = txt_thongtin.ReadOnly = txt_vga.ReadOnly = _enable;
            cb_NSX.Enabled = !_enable;
        }

        void Reset_Val()
        {
            int id = current_lap.Get_Lasted_ID();
            txt_Malaptop.Text = "Laptop" + (id + 1);
            txt_bh.Text = txt_CPU.Text = txt_Gia.Text = txt_hdd.Text
            = txt_monitor.Text = txt_Ram.Text = txt_Tenlaptop.Text = txt_thongtin.Text = txt_vga.Text = "";
            cb_NSX.SelectedIndex = 0;
        }

        void Get_Val()
        {
            current_lap.MaLaptop = txt_Malaptop.Text;
            current_lap.TenLaptop = txt_Tenlaptop.Text;
            current_lap.NhaSX = current_lap.NhaSX.Get_NSX_ByCode(cb_NSX.SelectedValue.ToString());
            current_lap.Baohanh = txt_bh.Text;
            current_lap.Cpu = txt_CPU.Text;
            current_lap.Gia = decimal.Parse(txt_Gia.Text);
            current_lap.Hdd = txt_hdd.Text;
            current_lap.Monitor = txt_monitor.Text;
            current_lap.Ram = txt_Ram.Text;
            current_lap.Thongtin = txt_thongtin.Text;
            current_lap.Vga = txt_vga.Text;
        }

        void Set_Val(Laptop_BIZ _laptop)
        {
            try
            {
                txt_bh.Text = _laptop.Baohanh;
                txt_CPU.Text = _laptop.Cpu;
                txt_Gia.Text = _laptop.Gia.ToString();
                txt_hdd.Text = _laptop.Hdd;
                txt_Malaptop.Text = _laptop.MaLaptop;
                txt_monitor.Text = _laptop.Monitor;
                txt_Ram.Text = _laptop.Ram.Trim();
                txt_Tenlaptop.Text = _laptop.TenLaptop;
                txt_thongtin.Text = _laptop.Thongtin;
                txt_vga.Text = _laptop.Vga;
                cb_NSX.SelectedValue = _laptop.NhaSX.MaNSX;
            }
            catch
            {
            }
        }

        private void btn_NSX_Click(object sender, EventArgs e)
        {
            NhaSX_GUI frm = new NhaSX_GUI();
            frm.ShowDialog();
        }

        private void txt_Gia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

    }
       
}
